﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PasswordManager
{
   [Serializable]
   class KeyPairGroup
   {
      protected string m_strName;
      protected List<KeyValuePair<string, string>> m_sItems;

      public KeyPairGroup()
      {
         m_strName = "";
         m_sItems = new List<KeyValuePair<string,string>>();
      }

      public string Name
      {
         get
         {
            return m_strName;
         }
         set
         {
            m_strName = value;
         }
      }

      public int Count
      {
         get
         {
            return m_sItems.Count();
         }
      }

      public bool Add(string strName, string strValue)
      {
         bool bRet = false;
         if (m_sItems.Exists(sItem => (sItem.Key.Equals(strName) == true)) == false)
         {
            KeyValuePair<string, string> sPair = new KeyValuePair<string, string>(strName, strValue);
            m_sItems.Add(sPair);
            bRet = true;
         }
         return bRet;
      }

      public bool Remove(string strName, string strValue)
      {
         bool bRet = false;
         if (m_sItems.Exists(sItem => (sItem.Key.Equals(strName) == true)) == true)
         {
            KeyValuePair<string, string> sPair = new KeyValuePair<string,string>(strName, strValue);
            m_sItems.Remove(sPair);
            bRet = true;
         }
         return bRet;
      }

      public bool ModifyKey(string strCurrKey, string strNewKey)
      {
         bool bRet = false;
         int lIdx = m_sItems.FindIndex(sItem => (sItem.Key.Equals(strCurrKey) == true));
         if (lIdx != -1)
         {
            KeyValuePair<string, string> sPair = new KeyValuePair<string, string>(strNewKey, m_sItems[lIdx].Value);
            m_sItems.RemoveAt(lIdx);
            m_sItems.Add(sPair);
            bRet = true;
         }
         return bRet;
      }

      public bool ModifyValue(string strKey, string strNewValue)
      {
         bool bRet = false;
         int lIdx = m_sItems.FindIndex(sItem => (sItem.Key.Equals(strKey) == true));
         if (lIdx != -1)
         {
            KeyValuePair<string, string> sPair = new KeyValuePair<string, string>(strKey, m_sItems[lIdx].Value);
            m_sItems.RemoveAt(lIdx);
            m_sItems.Add(sPair);
            bRet = true;
         }
         return bRet;
      }

      public void Clear()
      {
         m_sItems.Clear();
      }

      public KeyValuePair<string, string>[] List()
      {
         KeyValuePair<string, string>[] sTmpLst = null;
         if (m_sItems.Count() > 0)
         {
            sTmpLst = new KeyValuePair<string, string>[m_sItems.Count()];
            m_sItems.CopyTo(sTmpLst);
         }
         return sTmpLst;
      }

      public override bool Equals(object obj)
      {
         if (obj == null)
         {
            return false;
         }

         KeyPairGroup sObj = obj as KeyPairGroup;
         if (sObj == null)
         {
            return false;
         }

         return (sObj.Name.Equals(m_strName));
      }

      public override int GetHashCode()
      {
         int lHash = m_strName.GetHashCode();
         foreach (KeyValuePair<string, string> sPair in m_sItems)
         {
            lHash = lHash ^ sPair.Key.GetHashCode();
         }
         return lHash;
      }
   }
}
